package edu.pku.sei.act.ctrl.editpart;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy;

import edu.pku.sei.act.ctrl.editPolicy.ModelXYLayoutEditPolicy;
import edu.pku.sei.act.ctrl.figure.MoteFigure;
import edu.pku.sei.act.images.ArchiModelImageProvider;
import edu.pku.sei.act.model.mote.Mote;
import edu.pku.sei.gmp.controller.editpart.GMPNodeEditPart;

public class MoteEditPart extends GMPNodeEditPart{
	
	@Override
	protected IFigure createFigure() {
		return new MoteFigure();
	}
	
	@Override
	public IFigure getContentPane() {
		if (getFigure() instanceof MoteFigure) {
			return ((MoteFigure)getFigure()).getContentPane();
		}
		return super.getContentPane();
	}
	
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.LAYOUT_ROLE,
				new ModelXYLayoutEditPolicy());
	}
	
	private Mote getMote() {
		return (Mote) getModelElement();
	}
	
	public void handlePropertyChanged(PropertyChangeEvent event) {
		refreshVisuals();
		super.handlePropertyChanged(event);
	}

	
	protected void refreshVisuals() {
		Mote modelEle = this.getMote();
		MoteFigure figure = (MoteFigure) getFigure();
		figure.getNameLabel().setText(modelEle.getName());
		figure.getNameLabel().setIcon(ArchiModelImageProvider.getImage(ArchiModelImageProvider.MOTELARGE));
		super.refreshVisuals();
	}

}
